/**
 * 
 */
package com.et114.components.acegi.ldap;

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;

import com.et114.core.exception.CheckException;
import com.et114.modules.org.vo.FunctionVo;
import com.et114.modules.org.vo.RoleVo;
import com.et114.modules.org.vo.TitleMsgVo;
import com.et114.modules.org.vo.UserVo;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import org.acegisecurity.ldap.LdapTemplate;


import org.springframework.util.Assert;

/**
 * from ldap server
 * @author guanhw
 */
public class LdapOrgManager {
	protected Log logger = LogFactory.getLog( this.getClass( ) );
	private LdapTemplate acegiLdapTemplate ;
	
	public static String userSearchBase = "ou=people" ; 
	public static String groupSearchBase = "ou=group" ; 
	public static String titleSearchBase = "ou=title" ; 
	public static String roleSearchBase = "ou=roles" ; 
	public static String functionSearchBase = "ou=function" ; 
	public static String baseDn = "dc=mycompany,dc=com" ;

	public void setAcegiLdapTemplate ( LdapTemplate acegiLdapTemplate ) {
		this.acegiLdapTemplate = acegiLdapTemplate;
	}
	
	/**
	 * from ldap 
	 * @return
	 * @throws CheckException
	 */
	public UserVo getUser() throws CheckException {
		return null;
	}
	
	/**
	 * from ldap
	 * @return
	 * @throws CheckException
	 */
	public TitleMsgVo getTitle()  throws CheckException {
		return null;
	}
	
	/**
	 * from ldap  
	 * @return
	 * @throws CheckException
	 */
	public RoleVo getRole()  throws CheckException {
		return null;
	}
	
	/**
	 * from ldap
	 * @return
	 * @throws CheckException
	 */
	public FunctionVo getFunction() throws CheckException { 
		return null;
	}
	
	/**
	 * 
	 * @return
	 * @throws CheckException
	 */
	public List getUsers( String filter , String user_attr ) throws CheckException {
		List users = new ArrayList();
		Set set =  acegiLdapTemplate.searchForSingleAttributeValues( userSearchBase , filter , null ,  user_attr );
		UserVo  vo = null ; 
        Iterator it = set.iterator();
        while (it.hasNext()) {
        	vo = new UserVo();
            String username = ( String ) it.next(); // username
            //根据名字去相关属性
           /*
            Object obj = acegiLdapTemplate.searchForSingleEntry( "ou=people" , "(objectClass=inetOrgPerson)" , new String[] { "uid=guanhw,ou=people,dc=mycompany,dc=com" , "guanhw" } , new LdapUserDetailsMapper() );
            LdapUserDetailsImpl.Essence user = ( LdapUserDetailsImpl.Essence ) obj ;
            user.setUsername( username );
            LdapUserDetails lud = user.createUserDetails() ;
            vo.setPassword( lud.getPassword() ) ;
            */
            vo.setLoginName( username ) ; 
            vo.setPassword( "1" ) ; 
            users.add( vo ) ;
        }
		return users ;
	}
	
	/**
	 * 
	 * @param username  登录名
	 * @param dn        DN
	 * @param filter    过滤条件
	 * @param role_attr 取属性
	 * @return
	 * @throws CheckException
	 */
	public List getRolesByname( String searchBase ,String name , String dn , String filter , String role_attr ) throws CheckException {
		Assert.notNull( name  , "name不能为空！" ) ; 
		Assert.notNull( dn  , "dn不能为空！" ) ; 
		Assert.notNull( role_attr , "role_attr不能为空！" ) ; 
		List roles = new ArrayList( 1 ) ; 
		Set set =  acegiLdapTemplate.searchForSingleAttributeValues( searchBase , filter , new String[]{ dn , name } ,  role_attr );
		RoleVo vo = null ; 
        Iterator it = set.iterator();
        while ( it.hasNext() ) {
        	vo = new RoleVo();
        	String ss = ( String ) it.next();
        	vo.setRoleName( ss ) ; 
        	roles.add( vo ) ; 
        }		
		return roles;
	}
	
	/**
	 * 
	 * @param dn
	 * @return
	 * @throws CheckException
	 */
	public List getTitlesByname( String searchBase , String name , String dn , String filter , String title_attr ) throws CheckException {
		Assert.notNull( name  , "name不能为空！" ) ; 
		Assert.notNull( dn  , "dn不能为空！" ) ; 
		Assert.notNull( title_attr , "title_attr不能为空！" ) ; 
		List titles = new ArrayList( 1 ) ; 
		Set set =  acegiLdapTemplate.searchForSingleAttributeValues( searchBase , filter , new String[]{ dn , name } ,  title_attr );
		TitleMsgVo vo = null ; 
        Iterator it = set.iterator();
        while ( it.hasNext() ) {
        	vo = new TitleMsgVo();
        	String ss = ( String ) it.next();
        	vo.setTitleName(  ss ) ; 
        	titles.add( vo ) ; 
        }		
		return titles;
	}
	

	
	public List getUsersByname( String searchBase , String name , String dn , String filter , String user_attr ) throws CheckException { 
		Assert.notNull( name  , "name不能为空！" ) ; 
		Assert.notNull( dn  , "dn不能为空！" ) ; 
		Assert.notNull( user_attr , "user_attr不能为空！" ) ; 
		Assert.notNull( filter , "filter不能为空！" ) ; 
		List users = new ArrayList( 1 );
		Set set = acegiLdapTemplate.searchForSingleAttributeValues( searchBase , filter , new String[]{ dn , name } ,  user_attr );
		UserVo user = null ;
		Iterator it = set.iterator(); 
		while ( it.hasNext() ) {
			user = new UserVo ();
			String ss = ( String ) it.next();
			user.setLoginName( ss ) ; 
			users.add( user ) ; 
		}
		return users;
	}
	

	
	/**
	 * 
	 * @param filter
	 * @param func_attr
	 * @return
	 * @throws CheckException
	 */
	public List getFunctions ( String filter , String func_attr ) throws CheckException { 
		Assert.notNull( filter  , "filter不能为空！" ) ; 
		Assert.notNull( func_attr  , "func_attr不能为空！" ) ; 
		List funcs = new ArrayList( 1 ) ;
		Set set = acegiLdapTemplate.searchForSingleAttributeValues( functionSearchBase , filter , null , func_attr ) ;
		FunctionVo vo = null ; 
	    Iterator it = set.iterator();
        while ( it.hasNext() ) {
        	vo = new FunctionVo() ; 
        	String ss = ( String ) it.next();
        	vo.setUrl( ss ) ; 
        	funcs.add( vo ) ;
        }
		return funcs ;
	}
	
	
	
	public Set searchForSingleAttributeValues( String searchBase , String name , String dn , String filter , String user_attr ) throws CheckException { 
		Assert.notNull( name  , "name不能为空！" ) ; 
		Assert.notNull( dn  , "dn不能为空！" ) ; 
		Assert.notNull( user_attr , "user_attr不能为空！" ) ; 
		Assert.notNull( filter , "filter不能为空！" ) ; 
		return acegiLdapTemplate.searchForSingleAttributeValues( searchBase , filter , new String[]{ dn , name } ,  user_attr );
	}

	public String getUserSearchBase ( ) {
		return userSearchBase;
	}

	public void setUserSearchBase ( String userSearchBase ) {
		Assert.notNull( userSearchBase , "userSearchBase不能为空" ) ; 
		LdapOrgManager.userSearchBase = userSearchBase;
	}

	public String getGroupSearchBase ( ) {
		return groupSearchBase;
	}

	public void setGroupSearchBase ( String groupSearchBase ) {
		Assert.notNull( groupSearchBase , "groupSearchBase不能为空" ) ; 
		LdapOrgManager.groupSearchBase = groupSearchBase;
	}

	public String getTitleSearchBase ( ) {
		return titleSearchBase;
	}

	public void setTitleSearchBase ( String titleSearchBase ) {
		Assert.notNull( titleSearchBase , "titleSearchBase不能为空" ) ; 
		LdapOrgManager.titleSearchBase = titleSearchBase;
	}

	public String getRoleSearchBase ( ) {
		return roleSearchBase;
	}

	public void setRoleSearchBase ( String roleSearchBase ) {
		Assert.notNull( roleSearchBase , "roleSearchBase不能为空" ) ; 
		LdapOrgManager.roleSearchBase = roleSearchBase;
	}

	public String getFunctionSearchBase ( ) {
		return functionSearchBase;
	}

	public void setFunctionSearchBase ( String functionSearchBase ) {
		Assert.notNull( functionSearchBase , "functionSearchBase不能为空" ) ; 
		LdapOrgManager.functionSearchBase = functionSearchBase;
	}

	public String getBaseDn ( ) {
		return baseDn;
	}

	public void setBaseDn ( String baseDn ) {
		LdapOrgManager.baseDn = baseDn;
	}
	
}
